﻿CREATE PROCEDURE sp__SpaceUsedEx AS
CREATE TABLE #Res(name sysname,rows bigint,reserved bigint,data bigint,index_size bigint,unused bigint)
DECLARE @Tables TABLE(ID int identity,TN sysname)
INSERT INTO @Tables SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'

DECLARE @ID int,@TN sysname SELECT @ID = 1
WHILE 1 = 1 BEGIN
	SELECT @TN = TN, @ID = @ID+1 FROM @Tables WHERE ID = @ID
	IF @@ROWCOUNT = 0 BREAK
	INSERT INTO #Res exec sp__spaceused @TN
END

SELECT *,dbo.Format_Int(reserved) reserved_f,dbo.Format_Int(data) data_f,dbo.Format_Int(index_size)index_size,dbo.Format_Int(unused)unused FROM #Res ORDER BY reserved DESC
